package com.j1ay.forum.service.impl;

import com.j1ay.forum.model.pojo.vo.ErrorCode;
import com.j1ay.forum.model.pojo.vo.Result;
import com.j1ay.forum.model.pojo.vo.UploadVo;
import com.j1ay.forum.service.UploadSerive;
import com.j1ay.forum.service.UserService;
import com.j1ay.forum.utils.QiniuUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Objects;
import java.util.UUID;

/**
 * @Author J1ay
 * @Date 2021/12/21 20:09
 * @Description
 */
@Service
public class UploadServiceImpl implements UploadSerive {

    @Autowired
    private QiniuUtils qiniuUtils;

    @Autowired
    private UserService userService;

    @Override
    public Result uploadImg(MultipartFile file, Long userId) {
        if (file.isEmpty() || Objects.isNull(userId)) {
            return Result.fail(ErrorCode.PARAMS_BLANK.getCode(), ErrorCode.PARAMS_BLANK.getMsg());
        }
        String originalFileName = file.getOriginalFilename();
        String fileName =  UUID.randomUUID().toString()+"." + StringUtils.substringAfterLast(originalFileName, ".");
        boolean upload = qiniuUtils.upload(file, fileName);
        if (upload) {
            UploadVo uploadVo = new UploadVo();
            uploadVo.setFilename(fileName);
            uploadVo.setPrefix(QiniuUtils.url);
            uploadVo.setUrl(QiniuUtils.url + "/" +fileName);

            int res = userService.uploadTx(fileName, userId);

            if (0 == res) {
                return Result.fail(ErrorCode.SESSION_FAILED.getCode(), ErrorCode.SESSION_FAILED.getMsg());
            }

            return Result.success(uploadVo);
        }
        return Result.fail(444,"上传失败,稍后再试~");
    }


    @Override
    public Result uploadMdImg(MultipartFile file) {
        if (file.isEmpty()) {
            return Result.fail(ErrorCode.PARAMS_BLANK.getCode(), ErrorCode.PARAMS_BLANK.getMsg());
        }
        String originalFileName = file.getOriginalFilename();
        String fileName =  UUID.randomUUID().toString()+"." + StringUtils.substringAfterLast(originalFileName, ".");
        boolean upload = qiniuUtils.upload(file, fileName);
        if (upload) {
            UploadVo uploadVo = new UploadVo();
            uploadVo.setFilename(fileName);
            uploadVo.setPrefix(QiniuUtils.url);
            uploadVo.setUrl(QiniuUtils.url + "/" +fileName);
            return Result.success(uploadVo);
        }
        return Result.fail(444,"上传失败,稍后再试~");
    }

    @Override
    public Result downLoad(String filename) {
        if(Objects.isNull(filename)) {
            return Result.fail(ErrorCode.PARAMS_BLANK.getCode(), "找寻不到您要下载的文件~");
        }
        String url = qiniuUtils.download(filename);
        if (Objects.isNull(url)) {
            return Result.fail(ErrorCode.PARAMS_BLANK.getCode(), "下载失败，请稍后再试~");
        }
        return Result.success(url);
    }



}
